import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class RequestTimeMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    const startTime = Date.now();
    const { method, originalUrl } = req;

    res.on('finish', () => {
      const endTime = Date.now();
      const duration = endTime - startTime;
      const timestamp = new Date().toISOString();
      
      console.log(`[${timestamp}] 请求耗时: ${duration}ms | ${method} ${originalUrl}`);
    });

    next();
  }
}